from app.models.system_access_log_model import AccessLogModel
from app.schemas.access_log import AccessLogQueryParams
from app.schemas.common import PaginationParams, PaginationResult
from app.services.base import filter_query


class AdminAccessLogService:

    @classmethod
    async def get_access_logs(cls, params: AccessLogQueryParams, pagination: PaginationParams) -> PaginationResult:
        query = AccessLogModel.all()

        special_conditions = {
            "path": {"path__icontains": params.path},
            "ip": {"ip__icontains": params.ip},
            "start_time": {"created_at__gte": params.start_time},
            "end_time": {"created_at__lte": params.end_time},
        }

        query = await filter_query(query, params, special_conditions)
        total = await query.count()

        records = await query.offset(pagination.get_skip()).limit(pagination.page_size).order_by("-created_at")

        return PaginationResult(items=records, total=total, page=pagination.page, page_size=pagination.page_size)
